//
//  Contacts.swift
//  jiweiapp
//
//  Created by Kin Wong on 15/8/22.
//  Copyright (c) 2015年 Kin Wong. All rights reserved.
//

import UIKit

class Contacts: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var tableview: UITableView?
    var names = [String]()
    var phones = [String]()
    var xmlData = XMLAnalyser()
    
    //let animalIndexTitles = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
    
    override func loadView() {
        super.loadView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.whiteColor()
        (names, phones) = xmlData.readPhone("Phone")
        self.tableview = UITableView(frame: CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)-60), style: UITableViewStyle.Plain)
        self.tableview!.delegate = self
        self.tableview!.dataSource = self
        self.tableview!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "PhoneCell")
        self.tableview!.tableFooterView = UIView(frame: CGRectZero)
        self.view.addSubview(self.tableview!)
        // Do any additional setup after loading the view.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func numberOfSection(tableView: UITableView!) -> Int {
        return 1
    }
    
    /*func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        var headers = animalIndexTitles
        return headers[section]
    }*/
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.names.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let identify: String = "PhoneCell"
        
        let cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "PhoneCell")
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        cell.textLabel!.text = self.names[indexPath.row]
        cell.detailTextLabel?.text = self.phones[indexPath.row]
        
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.tableview?.deselectRowAtIndexPath(indexPath, animated: true)
    }

    /*func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]! {
        return animalIndexTitles
    }
    
    func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
        var index = 0
        for character in animalIndexTitles{
            if character == title{
                return index
            }
            index++
        }
        return 0
    }
    
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }*/
}
